package com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttMe

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.LinearLayout
import android.widget.Spinner
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.model.Attreneys.AttMeListAllModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttMe.adapter.AttMeListAdapter
import kotlinx.android.synthetic.main.activity_att_me.*
import kotlinx.android.synthetic.main.activity_att_show_list_all.*
import kotlinx.coroutines.Dispatchers

/**
 * 律师咨询
 */
class AttMeActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_att_me)
        title = "律师咨询"

        // 筛选
        setSpinner()

        // 启动线程
        runCoroutine()
    }

    /**
     * 协程管理
     */
    private fun runCoroutine() = coroutine(Dispatchers.IO){
        // 我的咨询列表
        meAll()
    }


    /**
     * 我的咨询列表
     *
     */
    private suspend fun meAll(){
       // 网络请求获取数据
        val ru = api.getAttMeListAll(getToken())
        // 判断是否拿到数据
        if (ru.code == 200){
            runOnUiThread {
                // 拿数据填充
                attMeListRecyclerView.apply {
                    this.isNestedScrollingEnabled = false
                    this.layoutManager = LinearLayoutManager(context)
                    this.adapter = AttMeListAdapter(ArrayList<AttMeListAllModel.RowsBean>().apply {
                        this.addAll(ru.rows!!)
                    }, R.layout.re_att_me_home_list)
                }

            }

        }else runOnUiThread { ru.msg?.show() }
    }





    /**
     * TODO 受理状态
     */
    private fun setSpinner() {
        // 适配器
        ArrayAdapter.createFromResource(
            this,
            R.array.spinner_AttMe,
            android.R.layout.simple_list_item_1
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            attMeSpinner.adapter = adapter
        }

        // 点击回调事件监听
        attMeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {

            }
        }
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}